using System.Drawing;
using GTA;
using NativeUI;

namespace CustomPeds.Menus;

public class MainMenu : Script
{
	public static UIMenu menu = new UIMenu("", TextSetting.main_Menu, new Point(0, -107));
	private static bool isMenuInitialized;

	public MainMenu()
	{
		if (!isMenuInitialized)
		{
			Main.pool.Add(menu);
			menu.SetBannerType(new UIResRectangle());
			menu.AddItem(new UIMenuItem(TextSetting.main_MenuItem_PlayerMenu));
			menu.AddItem(new UIMenuItem(TextSetting.main_MenuItem_GirlfriendMenu));
			menu.BindMenuToItem(PlayerMenu.menu, menu.MenuItems[0]);
			menu.BindMenuToItem(GirlfriendMenu.menu, menu.MenuItems[1]);
			menu.AddItem(new UIMenuItem(TextSetting.main_MenuItem_Interact, TextSetting.main_MenuItem_Interact_dct));
			menu.OnItemSelect += OnItemSelect;
			isMenuInitialized = true;
		}
	}

	private void OnIndexChange(UIMenu sender, int index)
	{
	}

	private void OnItemSelect(UIMenu sender, UIMenuItem item, int index)
	{
		switch (index)
		{
		case 0:
			PlayerMenu.InitPlayerMenu();
			// 选择子菜单时隐藏主菜单，但保持开关状态
			menu.Visible = false;
			Main.OnMainMenuHidden();
			break;
		case 1:
			GirlfriendMenu.InitGirlfriendMenu();
			// 选择子菜单时隐藏主菜单，但保持开关状态
			menu.Visible = false;
			Main.OnMainMenuHidden();
			break;
		case 2:
			if (!Main.P1.Character.IsInVehicle())
			{
				Main.m_Status = Main.MaleStatus.OnFoot;
				OnFootMainMenu.InitOnFootMainMenu();
				OnFootMainMenu.menu.Visible = true;
			}
			if (Main.P1.Character.IsInVehicle() && Main.IsPedDriverOfCurrentVehicle(Main.P1.Character))
			{
				Main.m_Status = Main.MaleStatus.Vehicle;
				VehicleMainMenu.InitVehicleMainMenu();
				VehicleMainMenu.menu.Visible = true;
			}
			// 选择子菜单时隐藏主菜单，但保持开关状态
			menu.Visible = false;
			Main.OnMainMenuHidden();
			break;
		}
	}

	private void OnMenuClose(UIMenu sender)
	{
		// 子菜单关闭时，如果主菜单应该显示，则重新显示
		if (Main.isMainMenuVisible && !menu.Visible)
		{
			menu.Visible = true;
		}
	}
}
